package io.shallwe.common.db;

import com.alibaba.druid.pool.DruidDataSource;
import com.google.common.collect.Sets;
import com.jfinal.log.Log;
import java.sql.SQLException;
import javax.sql.DataSource;
public class DruidDataSourceFactory implements DataSourceFactory {
    static Log log = Log.getLog(DruidDataSourceFactory.class);

    public DruidDataSourceFactory() {
    }

    public DataSource createDataSource(DataSourceConfig config) {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUrl(config.getUrl());
        druidDataSource.setUsername(config.getUser());
        druidDataSource.setPassword(config.getPassword());
        druidDataSource.setDriverClassName(config.getDriverClassName());
        druidDataSource.setMaxActive(config.getMaximumPoolSize());
        if (config.getMinimumIdle() != null) {
            druidDataSource.setMinIdle(config.getMinimumIdle());
        }

        if (config.getConnectionInitSql() != null) {
            druidDataSource.setConnectionInitSqls(Sets.newHashSet(new String[]{config.getConnectionInitSql()}));
        }

        try {
            druidDataSource.setFilters("stat");
        } catch (SQLException var4) {
            log.error("DruidDataSourceFactory is error", var4);
        }

        return druidDataSource;
    }
}